#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :百度图片.py
# @Time      :2024/5/24 
# @Author    :CL
# @email     :1037654919@qq.com
import time

import requests
import os
import re
from util import mongo_manager
from retrying import retry
images_url = mongo_manager('images_url',db = 'cl_data')


headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
@retry(wait_fixed=5,stop_max_attempt_number=3)
def get_images_from_baidu(keyword, pn):
    url = 'https://image.baidu.com/search/acjson?'
    headers = {
        "Accept": "text/plain, */*; q=0.01",
        "Accept-Language": "zh-CN,zh;q=0.9",
        "Connection": "keep-alive",
        "Referer": "https://image.baidu.com/search/index?tn=baiduimage&ct=201326592&lm=-1&cl=2&ie=gb18030&word=%CB%AB%C2%ED%CE%B2%C5%AE%20%CD%BC%C6%AC&fr=ala&ala=1&alatpl=normal&pos=0&dyTabStr=MTEsMCwzLDQsMSw2LDUsMiw3LDgsOQ%3D%3D",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-origin",
        "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
        "X-Requested-With": "XMLHttpRequest",
        "sec-ch-ua": "\"Not.A/Brand\";v=\"8\", \"Chromium\";v=\"114\", \"Google Chrome\";v=\"114\"",
        "sec-ch-ua-mobile": "?0",
        "sec-ch-ua-platform": "\"Linux\""
    }
    cookies = {
        "BDIMGISLOGIN": "0",
        "winWH": "%5E6_1919x974",
        "BDqhfp": "%E5%8F%8C%E9%A9%AC%E5%B0%BE%E5%A5%B3%20%E5%9B%BE%E7%89%87%26%26NaN-1undefined%26%26612%26%262",
        "BIDUPSID": "848C150FAC264FBD6FAF973D7760D525",
        "PSTM": "1688395393",
        "BAIDUID": "848C150FAC264FBD2D3F710F7CF988DB:FG=1",
        "indexPageSugList": "%5B%22%E9%B1%BC%E9%A6%99%E8%82%89%E4%B8%9D%22%5D",
        "cleanHistoryStatus": "0",
        "delPer": "0",
        "PSINO": "5",
        "ZFY": "yqWeKWhFyYHpauVjxH0Vxlo39H0sSdrWs8wrDXmcPl0:C",
        "BAIDUID_BFESS": "848C150FAC264FBD2D3F710F7CF988DB:FG=1",
        "H_PS_PSSID": "39319_39366_39354_39352_39407_39097_39413_39198_39346_39261_39358_39371_39233_39403_26350",
        "BA_HECTOR": "ah8125a0258g2h240k8h8g2b1igjelq1p",
        "BDORZ": "B490B5EBF6F3CD402E515D22BCDA1598",
        "BDRCVFR[dG2JNJb_ajR]": "mk3SLVN4HKm",
        "userFrom": "ala",
        "BDRCVFR[-pGxjrCMryR]": "mk3SLVN4HKm",
        "BDRCVFR[tox4WRQ4-Km]": "mk3SLVN4HKm",
        "ab_sr": "1.0.1_ODg4ZDU2ZjFmMDkxNjRjZGVmMjc0MzVlOGRlMzYzNDVlNDU2YzhkZDMyNDc2N2MyMDA3NjkxMWVjNDBlM2YwOWY2ZmUxZWVhYWZjNmIyYzAxMWNmNGQzNzdhYWU5ZmVkYmM1MzQ4ZTRiNGM1Zjg5Y2MxMDY3MWJhZmQyOTNmYzBkYzYxMWE2YjFkZTk1NzVjZjk4NTRjYThlNWIxZGNkMQ==",
        "BDRCVFR[A24tJn4Wkd_]": "mk3SLVN4HKm"
    }

    param = {'tn': 'resultjson_com',
             'logid': '7603311155072595725',
             'ipn': 'rj',
             'ct': 201326592,
             'is': '',
             'fp': 'result',
             'queryWord': keyword,
             'cl': 2,
             'lm': -1,
             'ie': 'utf-8',
             'oe': 'utf-8',
             'adpicid': '',
             'st': -1,
             'z': '',
             'ic': '',
             'hd': '',#高清
             'latest': '',
             'copyright': '',
             'word': keyword,
             's': '',
             'se': '',
             'tab': '',
             'width': '',
             'height': '',
             'face': 0,
             'istype': 2,
             'qc': '',
             'nc': '1',
             'fr': '',
             'expermode': '',
             'force': '',
             'cg': '',    # 这个参数没公开，但是不可少
             'pn': pn,    # 显示：30-60-90
             'rn': '30',  # 每页显示 30 条
             'gsm': '1e',
             '1618827096642': ''
             }
    request = requests.get(url=url, headers=headers,cookies=cookies, params=param,timeout=10)
    print(request.url,request)
    request.encoding = 'utf-8'
    # 正则方式提取图片链接
    html = request.text
    image_url_list = re.findall('"thumbURL":"(.*?)",', html, re.S)
    return image_url_list

@retry(wait_fixed=5,stop_max_attempt_number=3)
def get_image_data(image_url):
    response = requests.get(image_url, headers=headers,timeout=10)
    if response.status_code == 200:
        return response.content

if  __name__ == '__main__':
    print()

    word_20241108 = ['车内雨伞', '车内水杯', '车内喝醉', '车内打哈气', '车内咳嗽', '车内危险动作', '停车场',
                     '高速收费站', '拥堵道路', '道路施工', '车祸', '封路']
    word_20241113 = ['车内', '水杯', '雨伞']
    word_20241113_1 = ['在车内', '在车上', '车后排']
    word_20241114 = ['车内手机', '车内包', '车内办公', '车内钥匙', '车内充电宝', '车内宠物', '车内吃饭','车内打哈欠','车内抽烟','车内吸烟']
    word_20241114_1 = ['查看导航（车内）', '查看行车记录仪（车内）', '车内安排日程', '车内办公', '车内抱着狗', '车内唱歌', '车内吃播',
            '车内吃东西', '车内吃零食', '车内打电话', '车内对话', '车内放松心情', '车内分享趣闻', '车内给孩子讲故事',
            '车内观看视频', '车内嗨', '车内喝水', '车内驾驶位自拍', '车内交流感受', '车内进行冥想', '车内进行思考',
            '车内看电影', '车内看手机', '车内聊天', '车内拍摄风景', '车内派对', '车内清扫', '车内生气', '车内使用平板',
            '车内视频', '车内睡觉', '车内说话', '车内谈论新闻', '车内讨论工作', '车内跳舞', '车内听音乐', '车内痛哭',
            '车内玩游戏', '车内吸烟', '车内享受美食', '车内享受阳光', '车内写作业', '车内欣赏风景', '车内休息',
            '车内娱乐', '车内阅读书籍', '车内照顾孩子', '车内整理物品', '车内准备工作', '车内自嗨', '车内自拍',
            '车上（车内）化妆', '车上（车内）聊天', '车上（车内）睡觉', '抽烟（车内）', '出行自驾车内视频', '出游车内视频',
            '打电话（车内）', '调节空调（车内）', '调整座椅（车内）', '儿童车内', '观察路况（车内）', '行车测试（车内）',
            '进行车内运动', '两口子车上（车内）对话', '清理车内杂物', '设置驾驶模式（车内）', '使用车载充电器（车内）',
            '使用车载屏幕（车内）', '试驾（车内）', '网约车司机视频摆拍（车内）', '系安全带（车内）', '寻找停车位（车内）',
            '孕妇车内', '自驾车内过夜', '自驾车内睡觉']
    word_20241115 = ['狗狗在车内', '猫猫在车内']
    word_20241118 = ['放置的雨伞','收缩的雨伞']
    for keyword in word_20241118[:]:
        print(keyword)
        path = f'/media/chenglei3/77D014CED257D1411/images/baidu/{keyword}/'
        os.makedirs(path, exist_ok=True)
        xuhao = 1
        for page in range(1, 20):
            pn = page * 30
            images = get_images_from_baidu(keyword, pn)
            for image_url in images:
                # 下载图片
                if images_url.findOne({'_id': image_url,'status':'done'}):
                    continue
                suffix = image_url.split('f=')[-1].split('?')[0]     # 文件后缀
                if suffix not in ['JPEG','JPG']:
                    continue
                reslut ={'_id': image_url,'platform':'baidu','keyword':keyword}
                image_name = f"{xuhao}_{image_url.split('u=')[-1].split(',')[0]}"  # 文件名
                print(f'Downloading image {image_name}...{image_url}')
                # image_data = requests.get(image_url,headers=headers,proxies=proxies).content
                image_data = get_image_data(image_url)
                if image_data:
                    with open(path + image_name +'.'+suffix, 'wb') as f:
                        f.write(image_data)
                    reslut['status'] = 'done'
                else:
                    reslut['status'] = 'fail'
                # 保存数据库
                try:
                    images_url.insertOne(reslut)
                except:
                    images_url.updateOne({'_id': image_url},reslut)
                xuhao += 1
            if len(images) < 30:
                break